﻿Public Class frmLeaveDE
    Dim validator As PropertyValidator = New PropertyValidator
    Dim payrollManager As New PayrollManager
    Private EmployeeInfo As EmployeeInfo
    Private LeaveType As String
    Private Sub frmLeaveDE_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        ClearAllFields()
        EmployeeInfo = MainGlobalData.mEmployeeInfo
        LeaveType = MainGlobalData.mleavetype
        txtEmployeeName.Text = EmployeeInfo.last_name & "," & EmployeeInfo.first_name
        txtLeaveType.Text = LeaveType
        If Clipboard.GetData(DataFormats.Text) <> "" Then
            Try
                Dim leaveList As List(Of Leave) = payrollManager.getItemLeave(Clipboard.GetData(DataFormats.Text))
                For Each leave As Leave In leaveList
                    txtEmployeeName.Text = leave.employee_id
                    txtLeaveType.Text = leave.leave_type_id
                    dtpDateFrom.Text = leave.leave_from
                    dtpDateTo.Text = leave.leave_to
                    txtReason.Text = leave.reason
                    'NOTE: ONLY THE ADMIN CAN VIEW THE CHECKBOX
                    chkAproveLeave.Checked = (If((leave.status = "0"), False, True))
                Next
                btnSave.Text = "&Update"
                btnClear.Text = "&Cancel"
            Catch ex As Exception
                MsgBox("Can't load Web page" & vbCrLf & ex.Message)
            End Try
        End If
    End Sub

    Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
        Try
            Dim leaveObject As New Leave
            leaveObject.employee_id = mEmployeeInfo.employee_id
            leaveObject.leave_type_id = If((LeaveType = "Vacation Leave"), "0", "1")
            leaveObject.leave_from = dtpDateFrom.Text
            leaveObject.leave_to = dtpDateTo.Text
            leaveObject.reason = txtReason.Text
            'NOTE: ONLY THE ADMIN CAN VIEW THE CHECKBOX
            leaveObject.status = (If((chkAproveLeave.Checked), "1", "0"))
            leaveObject.id = Clipboard.GetData(DataFormats.Text)
            If Clipboard.GetData(DataFormats.Text) = "" Then
                payrollManager.insertLeave(leaveObject)
                MsgBox("Successfully saved.")
            Else
                payrollManager.updateItemLeave(leaveObject)
                MsgBox("Successfully updated.")
            End If
            ClearAllFields()
        Catch ex As Exception
            MsgBox("Can't load Web page" & vbCrLf & ex.Message)
        End Try
        Me.Close()
    End Sub

    Private Sub btnClear_Click(sender As System.Object, e As System.EventArgs) Handles btnClear.Click
        If Clipboard.GetData(DataFormats.Text) = "" Then
            validator.ResetControls(grpLeave)
        Else
            Me.Close()
        End If
    End Sub
    Public Function ClearAllFields()
        validator.ResetControls(grpLeave)
        btnSave.Text = "&Save"
        btnClear.Text = "&Clear"
        Return 0
    End Function
End Class